package cn.skcks.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

		if (request.getRequestURI().contains("login")) {
			return true;
		}

		HttpSession session = request.getSession();

		System.out.println(session.getAttribute("user"));

		if (session.getAttribute("user") != null) {
			return true;
		}

		// 未登录则跳转到登录页
		request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
		System.out.println("===== 重定向到登录页 =====");
		return false;
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

	}
}